package com.briup.estore.dao.impl;

import com.briup.estore.common.constant.ExceptionMsgEnum;
import com.briup.estore.common.exception.EstoreException;
import com.briup.estore.common.exception.EstoreSystemException;
import com.briup.estore.common.pojo.User;
import com.briup.estore.common.utils.JDBCUtils;
import com.briup.estore.dao.UserDao;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

/**
 * @Author briup-adam
 * @Date 2023/8/3 下午3:04
 * @Description
 **/

public class UserDaoImpl implements UserDao {

    @Override
    public User selectByUsername(String username) {
        User user = JDBCUtils.findOne("select id,username,password,phone,gender,birthday,register_time,default_address_id from es_user where username = ?;", User.class, username);
        return user;
    }

    @Override
    public void save(User user) {
        JDBCUtils.DML("insert into es_user(username,password,phone,gender,birthday,register_time)  values(?,?,?,?,?,?)", user.getUsername(), user.getPassword(), user.getPhone(), user.getGender(), user.getBirthday(), user.getRegisterTime());
    }

    @Override
    public User getUserById(int id) {
        User user = JDBCUtils.findOne("select id,username,password,phone,gender,birthday,register_time,default_address_id from es_user where id = ?;", User.class, id);
        return user;
    }


    @Override
    public void updateUser(User user) {
        JDBCUtils.DML("update es_user set username=?,phone=?,gender=?,birthday=? where id=?;", user.getUsername(), user.getPhone(), user.getGender(), user.getBirthday(), user.getId());

    }

    @Override
    public void updateUserPassword(String password, int id) {
        JDBCUtils.DML("update es_user set password=? where id=?;", password, id);
    }

    @Override
    public void setDefaultAddress(Integer userId, Integer addressId) {
        JDBCUtils.DML("update es_user set default_address_id =? where id=?;", addressId, userId);

    }

    @Override
    public void cancelDefaultAddress(Integer userId) {
        JDBCUtils.DML("update es_user set default_address_id = null where id=?;", userId);
    }
}
